using System.Reflection;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework.Config;

namespace demoapp
{
	using System;
	using System.Web;

	public class GlobalApplication : HttpApplication
	{
		public void Application_OnStart()
		{
			ActiveRecordStarter.Initialize(Assembly.GetExecutingAssembly(), ActiveRecordSectionHandler.Instance);
		}

		public void Application_OnEnd() 
		{
		}

		private void Application_BeginRequest()
		{
			HttpContext.Current.Items.Add("ar.sessionscope", new SessionScope(FlushAction.Never));
		}

		private void Application_EndRequest()
		{
			try
			{
				SessionScope scope = HttpContext.Current.Items["ar.sessionscope"] as SessionScope;
				if (scope != null)
				{
					scope.Dispose();
				}
			}
			catch (Exception ex)
			{
				HttpContext.Current.Trace.Warn("Error", "EndRequest: " + ex.Message, ex);
			}
		}
	}
}
